{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step One: Open an interactive Python session\n",
    "\n",
    "**Introduction**\n",
    "\n",
    "This is a common question presented on many Stata related discussion lists. Of all the occurances, [Stackoverflow](https://stackoverflow.com/questions/2536047/convert-stata-dta-file-to-csv-without-stata-software) has one of the best set of responses.\n",
    "\n",
    "**Option A, Install Python**\n",
    "\n",
    "If you have not already installed Python it is not difficult to do so. The official [Python](https://www.python.org/) website is your best place to start.\n",
    "\n",
    "**Option B, Go over to the *Try Jupyter* site**\n",
    "\n",
    "If you do not have, and do not want to install Pythin, there is another option: Located here: [Try Jupyter](http://jupyter.org/try) option at http://jupyter.org/try. (Click on the \"Try Jupyter with Python\" link).\n",
    "\n",
    "# Step Two : Import Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step Three: Open your dta file.\n",
    "When using hub the [Try Jupyter](http://jupyter.org/try) option at http://jupyter.org/try, consider depositing your dta file at an accessible http location so that it can be accessed online."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "cfile = pd.read_stata('http://www.stata-press.com/data/r15/auto2.dta')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step Four: Display the data frame.\n",
    "After display you can check your results. If working on a local computer you can [export to xls, csv,](https://github.com/adamrossnelson/StataQuickReference/blob/master/dtaconversion/ConvertDtaToXls.ipynb) etc.\n",
    "\n",
    "However, if you are using [Try Jupyter](http://jupyter.org/try) option at http://jupyter.org/try you will have to copy and paste the results to another application.\n",
    "\n",
    "Also, if forced to use a copy paste option the `pd.set_option('display.max_rows', 1000)` might be of interest. Modify the number of max rows to fit your use case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>make</th>\n",
       "      <th>price</th>\n",
       "      <th>mpg</th>\n",
       "      <th>rep78</th>\n",
       "      <th>headroom</th>\n",
       "      <th>trunk</th>\n",
       "      <th>weight</th>\n",
       "      <th>length</th>\n",
       "      <th>turn</th>\n",
       "      <th>displacement</th>\n",
       "      <th>gear_ratio</th>\n",
       "      <th>foreign</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AMC Concord</td>\n",
       "      <td>4099</td>\n",
       "      <td>22</td>\n",
       "      <td>Average</td>\n",
       "      <td>2.5</td>\n",
       "      <td>11</td>\n",
       "      <td>2930</td>\n",
       "      <td>186</td>\n",
       "      <td>40</td>\n",
       "      <td>121</td>\n",
       "      <td>3.58</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AMC Pacer</td>\n",
       "      <td>4749</td>\n",
       "      <td>17</td>\n",
       "      <td>Average</td>\n",
       "      <td>3.0</td>\n",
       "      <td>11</td>\n",
       "      <td>3350</td>\n",
       "      <td>173</td>\n",
       "      <td>40</td>\n",
       "      <td>258</td>\n",
       "      <td>2.53</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AMC Spirit</td>\n",
       "      <td>3799</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>12</td>\n",
       "      <td>2640</td>\n",
       "      <td>168</td>\n",
       "      <td>35</td>\n",
       "      <td>121</td>\n",
       "      <td>3.08</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Buick Century</td>\n",
       "      <td>4816</td>\n",
       "      <td>20</td>\n",
       "      <td>Average</td>\n",
       "      <td>4.5</td>\n",
       "      <td>16</td>\n",
       "      <td>3250</td>\n",
       "      <td>196</td>\n",
       "      <td>40</td>\n",
       "      <td>196</td>\n",
       "      <td>2.93</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Buick Electra</td>\n",
       "      <td>7827</td>\n",
       "      <td>15</td>\n",
       "      <td>Good</td>\n",
       "      <td>4.0</td>\n",
       "      <td>20</td>\n",
       "      <td>4080</td>\n",
       "      <td>222</td>\n",
       "      <td>43</td>\n",
       "      <td>350</td>\n",
       "      <td>2.41</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Buick LeSabre</td>\n",
       "      <td>5788</td>\n",
       "      <td>18</td>\n",
       "      <td>Average</td>\n",
       "      <td>4.0</td>\n",
       "      <td>21</td>\n",
       "      <td>3670</td>\n",
       "      <td>218</td>\n",
       "      <td>43</td>\n",
       "      <td>231</td>\n",
       "      <td>2.73</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Buick Opel</td>\n",
       "      <td>4453</td>\n",
       "      <td>26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10</td>\n",
       "      <td>2230</td>\n",
       "      <td>170</td>\n",
       "      <td>34</td>\n",
       "      <td>304</td>\n",
       "      <td>2.87</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Buick Regal</td>\n",
       "      <td>5189</td>\n",
       "      <td>20</td>\n",
       "      <td>Average</td>\n",
       "      <td>2.0</td>\n",
       "      <td>16</td>\n",
       "      <td>3280</td>\n",
       "      <td>200</td>\n",
       "      <td>42</td>\n",
       "      <td>196</td>\n",
       "      <td>2.93</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Buick Riviera</td>\n",
       "      <td>10372</td>\n",
       "      <td>16</td>\n",
       "      <td>Average</td>\n",
       "      <td>3.5</td>\n",
       "      <td>17</td>\n",
       "      <td>3880</td>\n",
       "      <td>207</td>\n",
       "      <td>43</td>\n",
       "      <td>231</td>\n",
       "      <td>2.93</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Buick Skylark</td>\n",
       "      <td>4082</td>\n",
       "      <td>19</td>\n",
       "      <td>Average</td>\n",
       "      <td>3.5</td>\n",
       "      <td>13</td>\n",
       "      <td>3400</td>\n",
       "      <td>200</td>\n",
       "      <td>42</td>\n",
       "      <td>231</td>\n",
       "      <td>3.08</td>\n",
       "      <td>Domestic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>Renault Le Car</td>\n",
       "      <td>3895</td>\n",
       "      <td>26</td>\n",
       "      <td>Average</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10</td>\n",
       "      <td>1830</td>\n",
       "      <td>142</td>\n",
       "      <td>34</td>\n",
       "      <td>79</td>\n",
       "      <td>3.72</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>Subaru</td>\n",
       "      <td>3798</td>\n",
       "      <td>35</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>2.5</td>\n",
       "      <td>11</td>\n",
       "      <td>2050</td>\n",
       "      <td>164</td>\n",
       "      <td>36</td>\n",
       "      <td>97</td>\n",
       "      <td>3.81</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>Toyota Celica</td>\n",
       "      <td>5899</td>\n",
       "      <td>18</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>2.5</td>\n",
       "      <td>14</td>\n",
       "      <td>2410</td>\n",
       "      <td>174</td>\n",
       "      <td>36</td>\n",
       "      <td>134</td>\n",
       "      <td>3.06</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>Toyota Corolla</td>\n",
       "      <td>3748</td>\n",
       "      <td>31</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9</td>\n",
       "      <td>2200</td>\n",
       "      <td>165</td>\n",
       "      <td>35</td>\n",
       "      <td>97</td>\n",
       "      <td>3.21</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68</th>\n",
       "      <td>Toyota Corona</td>\n",
       "      <td>5719</td>\n",
       "      <td>18</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>2.0</td>\n",
       "      <td>11</td>\n",
       "      <td>2670</td>\n",
       "      <td>175</td>\n",
       "      <td>36</td>\n",
       "      <td>134</td>\n",
       "      <td>3.05</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>VW Dasher</td>\n",
       "      <td>7140</td>\n",
       "      <td>23</td>\n",
       "      <td>Good</td>\n",
       "      <td>2.5</td>\n",
       "      <td>12</td>\n",
       "      <td>2160</td>\n",
       "      <td>172</td>\n",
       "      <td>36</td>\n",
       "      <td>97</td>\n",
       "      <td>3.74</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>VW Diesel</td>\n",
       "      <td>5397</td>\n",
       "      <td>41</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>3.0</td>\n",
       "      <td>15</td>\n",
       "      <td>2040</td>\n",
       "      <td>155</td>\n",
       "      <td>35</td>\n",
       "      <td>90</td>\n",
       "      <td>3.78</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>VW Rabbit</td>\n",
       "      <td>4697</td>\n",
       "      <td>25</td>\n",
       "      <td>Good</td>\n",
       "      <td>3.0</td>\n",
       "      <td>15</td>\n",
       "      <td>1930</td>\n",
       "      <td>155</td>\n",
       "      <td>35</td>\n",
       "      <td>89</td>\n",
       "      <td>3.78</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>VW Scirocco</td>\n",
       "      <td>6850</td>\n",
       "      <td>25</td>\n",
       "      <td>Good</td>\n",
       "      <td>2.0</td>\n",
       "      <td>16</td>\n",
       "      <td>1990</td>\n",
       "      <td>156</td>\n",
       "      <td>36</td>\n",
       "      <td>97</td>\n",
       "      <td>3.78</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>Volvo 260</td>\n",
       "      <td>11995</td>\n",
       "      <td>17</td>\n",
       "      <td>Excellent</td>\n",
       "      <td>2.5</td>\n",
       "      <td>14</td>\n",
       "      <td>3170</td>\n",
       "      <td>193</td>\n",
       "      <td>37</td>\n",
       "      <td>163</td>\n",
       "      <td>2.98</td>\n",
       "      <td>Foreign</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>74 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              make  price  mpg      rep78  headroom  trunk  weight  length  \\\n",
       "0      AMC Concord   4099   22    Average       2.5     11    2930     186   \n",
       "1        AMC Pacer   4749   17    Average       3.0     11    3350     173   \n",
       "2       AMC Spirit   3799   22        NaN       3.0     12    2640     168   \n",
       "3    Buick Century   4816   20    Average       4.5     16    3250     196   \n",
       "4    Buick Electra   7827   15       Good       4.0     20    4080     222   \n",
       "5    Buick LeSabre   5788   18    Average       4.0     21    3670     218   \n",
       "6       Buick Opel   4453   26        NaN       3.0     10    2230     170   \n",
       "7      Buick Regal   5189   20    Average       2.0     16    3280     200   \n",
       "8    Buick Riviera  10372   16    Average       3.5     17    3880     207   \n",
       "9    Buick Skylark   4082   19    Average       3.5     13    3400     200   \n",
       "..             ...    ...  ...        ...       ...    ...     ...     ...   \n",
       "64  Renault Le Car   3895   26    Average       3.0     10    1830     142   \n",
       "65          Subaru   3798   35  Excellent       2.5     11    2050     164   \n",
       "66   Toyota Celica   5899   18  Excellent       2.5     14    2410     174   \n",
       "67  Toyota Corolla   3748   31  Excellent       3.0      9    2200     165   \n",
       "68   Toyota Corona   5719   18  Excellent       2.0     11    2670     175   \n",
       "69       VW Dasher   7140   23       Good       2.5     12    2160     172   \n",
       "70       VW Diesel   5397   41  Excellent       3.0     15    2040     155   \n",
       "71       VW Rabbit   4697   25       Good       3.0     15    1930     155   \n",
       "72     VW Scirocco   6850   25       Good       2.0     16    1990     156   \n",
       "73       Volvo 260  11995   17  Excellent       2.5     14    3170     193   \n",
       "\n",
       "    turn  displacement  gear_ratio   foreign  \n",
       "0     40           121        3.58  Domestic  \n",
       "1     40           258        2.53  Domestic  \n",
       "2     35           121        3.08  Domestic  \n",
       "3     40           196        2.93  Domestic  \n",
       "4     43           350        2.41  Domestic  \n",
       "5     43           231        2.73  Domestic  \n",
       "6     34           304        2.87  Domestic  \n",
       "7     42           196        2.93  Domestic  \n",
       "8     43           231        2.93  Domestic  \n",
       "9     42           231        3.08  Domestic  \n",
       "..   ...           ...         ...       ...  \n",
       "64    34            79        3.72   Foreign  \n",
       "65    36            97        3.81   Foreign  \n",
       "66    36           134        3.06   Foreign  \n",
       "67    35            97        3.21   Foreign  \n",
       "68    36           134        3.05   Foreign  \n",
       "69    36            97        3.74   Foreign  \n",
       "70    35            90        3.78   Foreign  \n",
       "71    35            89        3.78   Foreign  \n",
       "72    36            97        3.78   Foreign  \n",
       "73    37           163        2.98   Foreign  \n",
       "\n",
       "[74 rows x 12 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pd.set_option('display.max_rows', 1000)\n",
    "cfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
